package caidan.data;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import caidan.res.Resource;
import jinyilw.swing.FontTools;
import jinyilw.common.TextTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.border.RTitledBorder;
import jinyilw.swing.visuals.layout.OneRowLayout;
import jinyilw.swing.visuals.layout.VEFlowLayout;
import jinyilw.swing.visuals.window.HintWindow;

public class SearchPane extends JPanel
{
	private static final long serialVersionUID = -4374520696600382093L;
	private ArrayList<Material> searchedMaterials = new ArrayList<>();
	private ArrayList<ResultPanel> resultPanels = new ArrayList<>();
	private JTextField searchField = new JTextField(10);
	private JButton searchButton = new JButton("搜索", Resource.smallSearchIcon);
	private JPanel contentPanel = new JPanel();
	private JScrollPane scrollPane = new JScrollPane(contentPanel);
	private RTitledBorder titledBorder = new RTitledBorder("搜索结果",
			TitledBorder.CENTER, TitledBorder.BELOW_TOP,
			FontTools.getCustomFont(14), Color.cyan);

	private MaterialPane materialPane;
	private JDialog dialog;

	public SearchPane(JDialog dialog)
	{
		super(new BorderLayout());
		this.dialog = dialog;
		JPanel northPanel = new JPanel();
		northPanel.add(new JLabel("输入要搜索的食材:"));
		northPanel.add(searchField);
		northPanel.add(searchButton);
		add(northPanel, BorderLayout.NORTH);
		add(scrollPane, BorderLayout.CENTER);
		contentPanel.setLayout(new VEFlowLayout());

		searchField.addActionListener((ActionEvent e) ->
		{
			search();
		});
		searchButton.addActionListener((ActionEvent e) ->
		{
			search();
		});
	}

	private void search()
	{
		String name = searchField.getText();
		if (TextTools.isBlank(name))
		{
			HintWindow.showHint("请输入搜索内容！", this);
			return;
		}
		clearContent();
		searchMaterial(name);
		if (searchedMaterials.size() == 0)
		{
			HintWindow.showHint("未搜索到相关食材！", this);
		} else
		{
			scrollPane.setBorder(titledBorder);
			int i = 0, size = resultPanels.size();
			ResultPanel resultPanel;
			for (Material material : searchedMaterials)
			{
				if (i < size)
				{
					resultPanel = resultPanels.get(i);
					i++;
				} else
				{
					resultPanel = new ResultPanel();
					resultPanels.add(resultPanel);
				}
				resultPanel.setMaterial(material);
				contentPanel.add(resultPanel);
			}
		}
		scrollPane.revalidate();
		scrollPane.repaint();
	}

	private void searchMaterial(String text)
	{
		String[] split = text.split(" ");
		for (String s : split)
			System.out.println(s);

		Material[] materials = new Material[split.length];
		for (int i = 0; i < split.length; i++)
			for (Material material : Material.allMaterials)
				if (material.getName().equals(split[i]))
				{
					searchedMaterials.add(material);
					materials[i] = material;
					break;
				}

		String name;
		for1:
		for (Material material : Material.allMaterials)
		{
			for (int i = 0; i < materials.length; i++)
				if (material == materials[i])
					continue for1;

			name = material.getName();
			for (String s : split)
				if (name.contains(s))
				{
					searchedMaterials.add(material);
					break;
				}
		}
	}

	public void setMaterialPane(MaterialPane materialPane)
	{
		this.materialPane = materialPane;
	}

	private void clearContent()
	{
		searchedMaterials.clear();
		contentPanel.removeAll();
		scrollPane.setBorder(null);
	}

	public void resetContent()
	{
		clearContent();
		searchField.setText("");
		if (scrollPane.isShowing())
		{
			scrollPane.revalidate();
			scrollPane.repaint();
		}
	}

	class ResultPanel extends JPanel
	{
		private static final long serialVersionUID = 2783086142849415120L;
		private JLabel nameLabel = new JLabel();
		private JButton selectButton = new JButton("选择");
		private Material material;

		public ResultPanel()
		{
			super(new OneRowLayout(8));
			setBorder(RBorderFactory.getDefaultBorder());
			add(nameLabel);
			add(selectButton);
			selectButton.addActionListener((ActionEvent e) ->
			{
				materialPane.setMaterial(material);
				dialog.dispose();
			});
		}

		public void setMaterial(Material material)
		{
			this.material = material;
			nameLabel.setText(material.getName());
		}
	}
}
